package org.cloud.admin.auth.controller;

import java.util.List;
import java.util.Set;

import org.cloud.admin.auth.holder.AppHolder;
import org.cloud.admin.auth.service.AuthService;
import org.cloud.admin.auth.service.SysMenuService;
import org.cloud.common.exception.SlothCloudException;
import org.cloud.common.model.R;
import org.cloud.common.util.StringUtils;
import org.cloud.module.common.constant.AdminConstants;
import org.cloud.module.common.entity.SysMenuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * @author wangyuan
 * @since 2019年6月5日 上午11:14:59
 *
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {
	
	@Autowired
	private SysMenuService sysMenuService;
	
	@Autowired
	private AuthService authService;

	/**
	 * 导航菜单
	 */
	@GetMapping("/nav")
	public R nav(){
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(AppHolder.getLoginedUserId());
		Set<String> permissions = authService.getUserPermissions(AppHolder.getLoginedUserId());
		return R.ok().put("menuList", menuList).put("permissions", permissions);
	}
	
	/**
	 * 所有菜单列表
	 */
	@GetMapping("/list")
	public List<SysMenuEntity> list(){
		List<SysMenuEntity> menuList = sysMenuService.list();
		for(SysMenuEntity sysMenuEntity : menuList){
			SysMenuEntity parentMenuEntity = sysMenuService.getById(sysMenuEntity.getParentMenuId());
			if(parentMenuEntity != null){
				sysMenuEntity.setParentMenuName(parentMenuEntity.getMenuName());
			}
		}

		return menuList;
	}
	
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@GetMapping("/select")
	public R select(){
		//查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();
		
		//添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setId(0L);
		root.setMenuName("一级菜单");
		root.setParentMenuId(-1L);
		root.setOpen(true);
		menuList.add(root);
		
		return R.ok().put("menuList", menuList);
	}
	
	/**
	 * 菜单信息
	 */
	@GetMapping("/info/{menuId}")
	public R info(@PathVariable("menuId") Long menuId){
		SysMenuEntity menu = sysMenuService.getById(menuId);
		return R.ok().put("menu", menu);
	}
	
	/**
	 * 保存
	 */
	@PostMapping("/save")
	public R save(@ModelAttribute SysMenuEntity menu){
		//数据校验
		verifyForm(menu);
		
		menu.setCreatedUser(String.valueOf(AppHolder.getLoginedUserId()));
		menu.setUpdatedUser(String.valueOf(AppHolder.getLoginedUserId()));
		
		sysMenuService.save(menu);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@PostMapping("/update")
	public R update(@ModelAttribute SysMenuEntity menu){
		//数据校验
		verifyForm(menu);
		
		SysMenuEntity oldMenu = sysMenuService.getById(menu.getId());
		oldMenu.setParentMenuId(menu.getParentMenuId());
		oldMenu.setMenuName(menu.getMenuName());
		oldMenu.setMenuUrl(menu.getMenuUrl());
		oldMenu.setMenuPerms(menu.getMenuPerms());
		oldMenu.setMenuType(menu.getMenuType());
		oldMenu.setMenuIcon(menu.getMenuIcon());
		oldMenu.setMenuOrderNum(menu.getMenuOrderNum());
		oldMenu.setUpdatedUser(String.valueOf(AppHolder.getLoginedUserId()));
		
		sysMenuService.updateById(oldMenu);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping("/delete/{menuId}")
	public R delete(@PathVariable("menuId") long menuId){
		//判断是否有子菜单或按钮
		List<SysMenuEntity> menuList = sysMenuService.queryListParentId(menuId);
		if(menuList.size() > 0){
			return R.error("请先删除子菜单或按钮");
		}

		sysMenuService.delete(menuId);

		return R.ok();
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(SysMenuEntity menu){
		if(StringUtils.isBlank(menu.getMenuName())){
			throw new SlothCloudException("菜单名称不能为空");
		}
		
		if(menu.getParentMenuId() == null){
			throw new SlothCloudException("上级菜单不能为空");
		}
		
		//菜单
		if(menu.getMenuType() == AdminConstants.MenuType.MENU.getValue()){
			if(StringUtils.isBlank(menu.getMenuUrl())){
				throw new SlothCloudException("菜单URL不能为空");
			}
		}
		
		//上级菜单类型
		int parentType = AdminConstants.MenuType.CATALOG.getValue();
		if(menu.getParentMenuId() != 0){
			SysMenuEntity parentMenu = sysMenuService.getById(menu.getParentMenuId());
			parentType = parentMenu.getMenuType();
		}
		
		//目录、菜单
		if(menu.getMenuType() == AdminConstants.MenuType.CATALOG.getValue() ||
				menu.getMenuType() == AdminConstants.MenuType.MENU.getValue()){
			if(parentType != AdminConstants.MenuType.CATALOG.getValue()){
				throw new SlothCloudException("上级菜单只能为目录类型");
			}
			return ;
		}
		
		//按钮
		if(menu.getMenuType() == AdminConstants.MenuType.BUTTON.getValue()){
			if(parentType != AdminConstants.MenuType.MENU.getValue()){
				throw new SlothCloudException("上级菜单只能为菜单类型");
			}
			return ;
		}
	}
}
